package com.example.lib_net.common

import androidx.lifecycle.LifecycleOwner
import com.example.lib_net.msgbus.LiveDataBus



/**
 * @author ytx 
 * @createDate 2022/8/8 14:40
 * @description
 *
 * @updateUser ytx
 * @updateDate 2022/8/8 14:40
 * @updateRemark
 *
 * @version 1.0.0
 */
abstract class ApiStateObserver {
    protected var owner:LifecycleOwner?=null

    fun register(){
        owner =  setLifeCycleOwner()
        if (owner == null) {
            throw IllegalStateException("Owner must be set before initialization")
        }
        //接受事件
        LiveDataBus.with<Int>(ConstantValue.NET_STATE).observe(owner!!){
            apiStateChanged(it)
        }
    }

    /**
     * api 状态 改变 如：401 500等
     */
    abstract protected fun apiStateChanged(it: Int)

    /**
     * 设置生命周期所有者
     */
    abstract protected fun setLifeCycleOwner(): LifecycleOwner


}